import scipy.interpolate
import scipy.integrate as integrate


def to_float(array):
    return list(map(float, array))


def interpolateDataFromFile(file):
    lines = [" ".join(line.strip().split()).split(" ") for line in file]
    lines = list(map(to_float, lines))
    array_x = []
    array_y = []
    for line in lines:
        array_x.append(12395 / line[0])
        array_y.append(line[1])
    return scipy.interpolate.interp1d(array_x, array_y, fill_value='extrapolate')


def calibration(microns):
    with open('../calibration/FPU-function.txt', 'r') as file:
        fpu = interpolateDataFromFile(file)

    with open('../calibration/' + str(microns) + 'microns.txt', 'r') as file:
        Be = interpolateDataFromFile(file)

    calibrate = lambda x: fpu(x) * Be(x)
    return integrate.quad(calibrate, 0, 1000)[0]
